package com.moonciki.artadmin.core.tools;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moonciki.artadmin.core.model.PageData;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

@Data
@ApiModel("自动分页类")
public class CustomPageHelper<T> implements Serializable {
    public static final int default_page_index = 0;
    public static final int default_page_size = 0;

    @ApiModelProperty(value = "当前页码")
    private Integer pageNumber;

    @ApiModelProperty(value = "每页显示页数")
    private Integer pageSize;

    @ApiModelProperty(value = "总行数")
    private Long recordCount;

    @ApiModelProperty(value = "总页数")
    private Integer pageCount;

    @ApiModelProperty(value = "排序")
    private String orderBy;

    @ApiModelProperty(value = "查询分页结果")
    private List<T> dataList;

    /** pagger **/
    @JSONField(serialize=false)
    @JsonIgnore
    private Page pagger;

    /**
     * 计算页数
     */
    public void initPageCount(){
        int totalPage = 0;

        if(pageSize != null && pageSize > 0){

            double countTmp = recordCount;
            double sizeTmp = pageSize;

            totalPage = (int) Math.ceil(countTmp / sizeTmp);

        }

        this.setPageCount(totalPage);
    }

    public CustomPageHelper() {
        initPagger(null, null);
    }

    /**
     * 适用于 自带的分页方式
     * @param pageNumber    当前页码
     * @param pageSize      页容量
     */
    public CustomPageHelper(Integer pageNumber, Integer pageSize){
        initPagger(pageNumber, pageSize);
    }

    /**
     * 适用于 表格插件的分页
     * @param offset    开始分页位置
     * @param pageSize     分页大小
     */
    public void pageWithOffset(Integer offset, Integer pageSize){
        Integer pageNumber = offset / pageSize + 1;

        initPagger(pageNumber, pageSize);
    }

    /**
     * 初始化
     */
    public void initPagger(){
        initPagger(this.pageNumber, this.pageSize);
    }

    /**
     * 初始化
     * @param pageNumber
     * @param pageSize
     */
    public void initPagger(Integer pageNumber, Integer pageSize){
        if(pageNumber == null){
            pageNumber = default_page_index;
        }
        if(pageSize == null){
            pageSize = default_page_size;
        }

        setPageNumber(pageNumber);
        setPageSize(pageSize);

    }

    /**
     * 开始分页，查询之前 生成pagger
     * 调用之后必须跟 sql 查询语句、afterQuery() 或 cleanPage() 其中的一种
     * @return
     */
    public Page startPagger(){
        if (StringUtils.isNotBlank(orderBy) && pageSize <= 0) {
            PageHelper.orderBy(orderBy);
            pagger = PageHelper.getLocalPage();
        }else{
            pagger = PageHelper.startPage(pageNumber, pageSize, orderBy);
        }
        return pagger;
    }

    /**
     * 查询完毕，设置结果和页数
     */
    public PageData<T> endPagger(List<T> dataList){
        PageHelper.clearPage();

        this.dataList = dataList;

        if(pagger != null){
            recordCount = pagger.getTotal();
        }
        initPageCount();

        PageData<T> pageData = this.getPageData();

        return pageData;
    }

    /**
     * 清除 ThreadLocal 分页参数
     */
    public void cleanPage(){
        PageHelper.clearPage();
    }

    /**
     * 返回分页数据
     * @return
     */
    public PageData<T> getPageData(){
        PageData<T> pageData = new PageData(pageNumber, pageSize, recordCount, pageCount, dataList);
        return pageData;
    }

}
